out=../out/kernel
boot=$(out)/kernel.out
address=0xc400

OBJCOPY	:= $(GCCPREFIX)objcopy
OBJDUMP	:= $(GCCPREFIX)objdump
OBJDIR=.
CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 
CFLAGS += -fno-omit-frame-pointer
CFLAGS += -Wall -Wno-format -Wno-unused  -gstabs -m32  -fno-stack-protector
LDFLAGS = -m elf_i386

entry_objs= $(out)/entry.o
kernel_objs=$(out)/main.o $(out)/screen.o $(out)/font.o $(out)/print.o $(out)/idt_gdt.o $(out)/interrupt.o $(out)/asmint32.o
$(out)/%.o:%.c
	mkdir -p $(out)
	gcc -nostdinc $(CFLAGS) -Os -c -o $@ $<

$(out)/%.o:%.S
	mkdir -p $(out)
	gcc -nostdinc $(CFLAGS) -c -o $@ $<

$(out)/%.o:$(out)/%.asm
	nasm  -f elf -o $@ $<

all:entry main
	cat $(out)/main >> $(out)/entry
	cp $(out)/entry $(boot)

entry:$(entry_objs)
main:$(kernel_objs)
entry:
	ld $(LDFLAGS) -N -e entry -Ttext $(address) -o $(out)/$@.out $^
	objdump -S -D $(out)/$@.out >$(out)/$@.asm
	objcopy -S -O binary -j .text $(out)/$@.out $(out)/$@

main:
	@echo "addr=$(address)"
	ld $(LDFLAGS) -N -e os_main -T kernel.ld -o $(out)/$@.out $^
	objdump -S -D $(out)/$@.out > $(out)/$@.asm
	objcopy  -S -O binary  $(out)/$@.out $(out)/$@

